import logging; logging.basicConfig(level=logging.INFO)

import asyncio, os, json, time
from datetime import datetime

from aiohttp import web

def index(request):
    return web.Response(body=b'<h1>Awesome</h1>', content_type='text/html') #lvj 逗号后的后增加的，不加的话，浏览器访问ip，会提示下载

async def init(event_loop):
    # 创建web服务器实例app
    app = web.Application()
    app.router.add_route('GET', '/', index)

    # 利用event_loop.create_server()创建TCP服务
    runner = web.AppRunner(app)

    #方法2
    await runner.setup()
    site = web.TCPSite(runner, '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    await site.start()

    #方法1
    # srv = await event_loop.create_server(runner.app.make_handler(), '127.0.0.1', 9000)
    # logging.info('server started at http://127.0.0.1:9000...')
    # return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()